<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 15:29
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\HomeTemplateStencilGoodsCategoryModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class HomeTemplateCategoryService
 * @package SDXApi\Service
 */
class HomeTemplateCategoryService extends TemplateAbstract
{
	/**
	 * @var HomeTemplateStencilGoodsCategoryModel
	 */
	protected $templateStencilGoodsCategoryModel;

	/**
	 * @var GoodsRepository
	 */
	protected $goodsRepository;

	/**
	 * HomeTemplateCategoryService constructor.
	 */
	public function __construct()
	{
		$this->templateStencilGoodsCategoryModel = new HomeTemplateStencilGoodsCategoryModel();
		$this->goodsRepository = new GoodsRepository();
	}

	/**
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($stencil = array(), $userId = 0, $wid = 8)
	{
		$userRank = $this->getUserRank($userId);
		$cacheName = 'home_template_category_id_' . $stencil['id'] . 'rank_' . $userRank . '_wid_' . $wid;

//		获取缓存
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->stencilCategory($stencil, $userId, $wid);
			$this->cacheData($cacheName, $data, mt_rand(3600, 4300));
		}

		return $data;
	}

	/**
	 * 处理分类商品
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	protected function stencilCategory($stencil = array(), $userId = 0, $wid = 8)
	{
		if (empty($stencil)) {
			return false;
		}

		$this->goodsRepository->userId = $userId;
		$this->goodsRepository->wid = $wid;

		$categoryInfo = $this->findCategoryByStencilId($stencil['id']);

		$goods = $this->checkGoodsCategory($categoryInfo, $wid);

		if (empty($goods)) {
			$goods = array();
		}

		$stencil['goods_list'] = $goods;
		$stencil['type'] = 4;

		return $stencil;
	}

	/**
	 * 查找模块下的分类
	 * Created by Jerry
	 * @param $id
	 * @return bool|mixed
	 */
	protected function findCategoryByStencilId($id)
	{
		if (empty($id)) {
			return false;
		}

		return $this->templateStencilGoodsCategoryModel->findCategoryByStencilId($id);
	}

	/**
	 * 处理商品分类
	 * Created by Jerry
	 * @param $category
	 * @param $wid
	 * @return array|bool
	 */
	protected function checkGoodsCategory($category, $wid)
	{
		$goods = $this->findCategoryGoodsByCategoryId($category['category_id'], $category['goods_where'], $wid);

		$goodsList = $this->goodsRepository->formatGoodsTransform($goods);

		if (count($goodsList) > 1) {
			$goodsList = self::checkDataSort($goodsList);
		}

		$goodsList = array_merge($goodsList);

		return $goodsList;

	}

	/**
	 * 搜索分类下的所有商品
	 * Created by Jerry
	 * @param $category_id
	 * @param $goods_where
	 * @param int $wid
	 * @return bool|mixed
	 */
	protected function findCategoryGoodsByCategoryId($category_id, $goods_where, $wid = 8)
	{
		return $this->goodsRepository->findCategoryGoodsByCategoryId($category_id, $goods_where, $wid);
	}

	/**
	 * 排序
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @return mixed
	 */
	static protected function checkDataSort($data, $sortKey = 'sort')
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value[$sortKey];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}
}